package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.ICheckInCarDao;
import com.eling.elcms.travel.model.*;
import com.eling.elcms.travel.model.Car.CarStatus;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.CheckInImplement.DataSource;
import com.eling.elcms.travel.model.Driver.DriverStatus;
import com.eling.elcms.travel.service.ICheckInCarManager;
import com.eling.elcms.travel.service.ICheckInImplementManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CheckInCarManagerImpl extends GenericManagerImpl<CheckInCar, Long> implements ICheckInCarManager{
	ICheckInCarDao carDao;

	@Autowired
	private CarDispatchManagerImpl IcarMangerImp;
	
	@Autowired
	private ICheckInImplementManager cIImplementM;
	
	@Autowired
	private CarManagerImpl carImpl;
	
	@Autowired
	private DriverManagerImpl driverImpl;
	
	@Autowired
	private IBusinessMessageManager businessMessageManager;
	
	@Autowired
	public void setDao(ICheckInCarDao dao) {
        this.dao = dao;
        this.carDao = dao;
    }
	
	@Override
	public CheckInCar confirm(CheckInCar checkInCar,CarDispatchBill carDispatchBill){
		checkInCar.setStatus(CheckStatus.Pended);
		checkInCar = save(checkInCar);
		
		if(carDispatchBill.getUnitsCar()){
			//改变车的状态
			Car car = carImpl.get(carDispatchBill.getCar().getPkCar());
			car.setCarStatus(CarStatus.Running);
			carImpl.save(car);
			
			//改变司机状态
			Driver driver = driverImpl.get(carDispatchBill.getDriver().getPkDriver());
			driver.setDriverStatus(DriverStatus.Passage);
			driverImpl.save(driver);
		}
		
		CarDispatchBill dispatchBill = IcarMangerImp.save(carDispatchBill);
		//将车辆调度单会写到入住准备落实单
		CheckInImplement checkInImplement =checkInCar.getCheckInImplement();
//		checkInImplement.setCarDispatchBill(new HashSet<CarDispatchBill>(Arrays.asList(new CarDispatchBill[]{dispatchBill})));
		
		Set<CarDispatchBill> carDispatchBills = checkInImplement.getCarDispatchBill();
		
		List<CarDispatchBill> listdispatchBill = new ArrayList<>(carDispatchBills);
		listdispatchBill.add(dispatchBill);
		checkInImplement.setCarDispatchBill(new HashSet<>(listdispatchBill));
		cIImplementM.save(checkInImplement);
		// 设置入住准备落实单总表的已落实状态
		if (checkInCar.getStatus() != null && checkInCar.getStatus() == CheckStatus.Pended){
			cIImplementM.setCiiCheckStatusConfirm(checkInCar.getCheckInImplement());
			businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.CarDispatchBill,checkInImplement.getPkCIImplement()));
			businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.MemCheckIn,checkInImplement.getPkCIImplement()));
		}
		
		return checkInCar;
	}

	@Override
	public List<CheckInCar> queryAll(CheckInCar car, Date startDate, Date endDate) {
		List<CheckInCar> returnList = new ArrayList<>();
		
		//查询机构、联盟团的入住准备车辆落实单
		PropertyUtils.setProperty(car, "checkInImplement.groupReserve.checkInDate", startDate);
		PropertyUtils.setProperty(car, "checkInImplement.groupReserve.checkInDateEnd", endDate);
		List<CheckInCar> cicOneList = carDao.query(car);
		if(!cicOneList.isEmpty()){
			returnList.addAll(cicOneList);
		}
		
		//查询散客团的入住准备车辆落实单
		PropertyUtils.setProperty(car, "checkInImplement.groupReserve", null);
		PropertyUtils.setProperty(car, "checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate", startDate);
		PropertyUtils.setProperty(car, "checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDateEnd", endDate);
		List<CheckInCar> cicTwoList = carDao.query(car);
		if(!cicTwoList.isEmpty()){
			returnList.addAll(cicTwoList);
		}
		Collections.sort(returnList,new Comparator<CheckInCar>() {
			public int compare(CheckInCar o1, CheckInCar o2) {
				Date date1 = null;
				Date date2 = null;
				//散客
				if(o1.getCheckInImplement().getDataSource()==DataSource.NonMember){
					date1 = o1.getCheckInImplement().getNonMemGroupReserve().getNonMemberGroup().getCheckInDate();
				}else{//老人团
					date1 = o1.getCheckInImplement().getGroupReserve().getCheckInDate();
				}
				//散客
				if(o2.getCheckInImplement().getDataSource()==DataSource.NonMember){
					date2 = o2.getCheckInImplement().getNonMemGroupReserve().getNonMemberGroup().getCheckInDate();
				}else{//老人团
					date2 = o2.getCheckInImplement().getGroupReserve().getCheckInDate();
				}
				return date1.compareTo(date2);
			};
		});
		
		return returnList;
	}
}
